﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using Maticsoft.Common;


namespace WEB.workPlan
{
    public partial class workPlanFView : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        T_workPlan bll = new T_workPlan();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                tbTimestart.Text = DateTime.Now.ToShortDateString() .ToString();
                tbTimestop.Text = DateTime.Now.AddMonths(1).ToShortDateString();
                this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");//日期控件
                this.tbTimestop  .Attributes.Add("onClick", "new WdatePicker()");//日期控件
                tbTimestart.Enabled = false;
                ds = bll.GetList(Query());
                gvPlan.DataSource = ds ;
                gvPlan.DataBind();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            //DataSet ds = new DataSet();
            if (tbTimestart.Text != "" && tbTimestop.Text != "")
            {
                if (Convert.ToDateTime(tbTimestart.Text) > Convert.ToDateTime(tbTimestop.Text))
                {
                    MessageBox.Show(this, "结束日期不能大于开始日期！");
                    return;
                }
                else if (Convert.ToDateTime(tbTimestart.Text) == Convert.ToDateTime(tbTimestop.Text))
                {
                    MessageBox.Show(this, "结束时间不能等于开始时间");
                    return;
                }
                //ds = bll.GetList(Query ());
                //gvPlan.DataSource = ds;
                //gvPlan.DataBind();
            }
            ds = bll.GetList(Query());
            gvPlan.DataSource = ds;
            gvPlan.DataBind();
        }

        //查询条件
        public string Query()
        {
            string strQuery = ddlQuery.SelectedValue;
            string strSql = " 1=1 ";
            if (tbQuery.Text.Trim() != "")
            {
                switch (ddlQuery.SelectedIndex)  //根据选项传不同的SQL语句
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                        strSql += string.Format(" and {0} like '%{1}%' ", strQuery, tbQuery.Text.Trim());
                        break;
                    default: 
                        if (!PageValidate.IsNumber(tbQuery.Text.Trim())) 
                            tbQuery.Text = "0";
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                        break;
                }
            }
            //日期搜索的判断
            if (tbTimestart.Text != "")
            {
                DateTime timeStart = Convert.ToDateTime(tbTimestart.Text.Trim());
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimestop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    strSql = strSql + " and planDate between '" + timeStart.AddDays(1) + "' and '" + timeStop + "'";
                }
                else
                {
                    DateTime timeStop = Convert.ToDateTime(tbTimestop.Text.Trim());
                    strSql = strSql + " and planDate between '" + timeStart.AddDays(1) + "' and '" + timeStop + "'";
                }
            }
            strSql += " order by planDate  " + ddlasc.SelectedValue;
            return strSql;
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlasc_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvPlan.DataBind();
        }
        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvPlan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string index = e.CommandArgument.ToString();
            if (e.CommandName  == "Alter")
            {
                int id = Convert.ToInt32(gvPlan.DataKeys[int.Parse(index)].Value);
                Response.Redirect("workPlanEdit.aspx?ID="+id);
            }
            if (e.CommandName == "go")
            {
                
                    TextBox tb = (TextBox)gvPlan.BottomPagerRow.FindControl("inPageNum");
                    int num = Int32.Parse(tb.Text);
                    GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                    gvPlan_PageIndexChanging(null, ea);
              

            }
        }

  

        //只有未完成的才可以编辑
        protected void gvPlan_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddl = e.Row.Cells[11].FindControl("ddl") as DropDownList;//取得控件 
                if (ds.Tables.Count >0)
                {
                    string value = ds.Tables[0].Rows[e.Row.RowIndex]["states"].ToString();
                    if (value != "1")
                    {
                        Button bt = (Button)e.Row.Cells[1].Controls[0].FindControl("bt_change");
                        bt.Enabled = false;
                    }
                    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));//下拉框值得绑定
                    ddl.Enabled = false;
                }
            }
        }

        //换页
        protected void gvPlan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvPlan.PageIndex = e.NewPageIndex;//绑定最新页
            ds = bll.GetList(Query());
            gvPlan.DataSource = ds;
            gvPlan.DataBind();
            TextBox tb = (TextBox)gvPlan.BottomPagerRow.FindControl("inPageNum");
            tb.Text = (gvPlan.PageIndex + 1).ToString();


        }


    }
}